package com.ruoyi.workflow.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.workflow.domain.CarPackageAudit;
import com.ruoyi.workflow.service.IBpmnNodeService;
import com.ruoyi.workflow.service.ICarPackageAuditService;
import com.ruoyi.workflow.service.ITodoTaskService;
import com.ruoyi.workflow.vo.CarPackageAuditVO;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TodoTaskServiceImpl implements ITodoTaskService {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    @Autowired
    private IBpmnNodeService bpmnNodeService;
    @Override
    public TableDataInfo selectTodoList(CarPackageAudit carPackageAudit) {

        // 创建分页对象
        TableDataInfo info = new TableDataInfo();
        info.setCode(HttpStatus.SUCCESS);
        info.setMsg("查询成功");

        // 获取当前登录用户的名字
        String username = SecurityUtils.getUsername();
        // 基于当前登录用户,去查询该用户为候选人/负责人的任务数量
        long total = taskService.createTaskQuery()
                .taskCandidateOrAssigned(username)
                .count();
        info.setTotal(total);
        // 如果数量为 0 ,直接返回空数据
        if (total == 0){
            info.setRows(Collections.emptyList());
            return info;
        }

        // 接收前端的分页条件
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int startIndex = (pageDomain.getPageNum() - 1) * pageDomain.getPageSize();

        List<Task> taskList = taskService.createTaskQuery()
                .taskCandidateOrAssigned(username)
                .orderByTaskCreateTime()
                .asc()
                .listPage(startIndex, pageDomain.getPageSize());

        List<CarPackageAudit> aduitList = new ArrayList<>();
        // 6. 遍历任务列表
        for (Task task : taskList) {
            // // 7. 获取该任务的流程实例 id, 查询流程实例对象, 得到业务标识
            ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId())
                    .singleResult();
            CarPackageAudit audit = carPackageAuditService.getById(instance.getBusinessKey());
            CarPackageAuditVO vo = new CarPackageAuditVO();
            BeanUtils.copyBeanProp(vo,audit);
            // 为审核记录设值当前任务名称/ id ,当前审核人
            vo.setTaskId(task.getId());
            vo.setTaskName(task.getName());

            List<SysUser> userList = bpmnNodeService.selectUserByKey(task.getTaskDefinitionKey());
            List<String> nickNames = userList.stream().map(SysUser::getNickName).collect(Collectors.toList());

            vo.setAuditorys(JSON.toJSONString(nickNames));
            // 讲当前审核记录对象放入返回的集合中
            aduitList.add(vo);
        }
        // 最终返回审核记录列表集合
        info.setRows(aduitList);
        return info;
    }
}
